﻿namespace UnityEditor
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using UnityEngine;

    [StructLayout(LayoutKind.Sequential)]
    internal sealed class WebScriptObject : IDisposable
    {
        private IntPtr webScriptObjectWrapper;
        public void Dispose()
        {
            this.DestroyScriptObject();
        }

        ~WebScriptObject()
        {
            this.DestroyScriptObject();
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private extern void DestroyScriptObject();
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern object MonoObjectFromJSON(string json);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern WebScriptObject EvalJavaScript(string script);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern WebScriptObject InvokeMethodArray(string method, object[] args);
        public WebScriptObject InvokeMethod(string method, params object[] args)
        {
            return this.InvokeMethodArray(method, args);
        }

        public WebScriptObject Invoke(params object[] args)
        {
            object[] array = new object[args.Length + 1];
            args.CopyTo(array, 1);
            array[0] = this;
            return this.InvokeMethodArray("call", array);
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private extern WebScriptObject Internal_GetKey(string key);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private extern void Internal_SetKey(string key, object value);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern void Remove(string key);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private extern WebScriptObject Internal_GetIndex(int i);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private extern void Internal_SetIndex(int i, object value);
        public WebScriptObject Get(int key)
        {
            return this.Internal_GetIndex(key);
        }

        public WebScriptObject Get(string key)
        {
            WebScriptObject obj2 = this;
            char[] separator = new char[] { '.' };
            foreach (string str in key.Split(separator))
            {
                obj2 = obj2.Internal_GetKey(str);
                if (obj2 == null)
                {
                    return obj2;
                }
            }
            return obj2;
        }

        public T Get<T>(int key)
        {
            return this.Internal_GetIndex(key).Convert<T>();
        }

        public T Get<T>(string key)
        {
            return this.Get<WebScriptObject>(key).Convert<T>();
        }

        public void Set<T>(int key, T value)
        {
            this.Internal_SetIndex(key, value);
        }

        public void Set<T>(string key, T value)
        {
            this.Internal_SetKey(key, value);
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private extern object ConvertToType(System.Type t);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private extern bool IsValid();
        public T Convert<T>()
        {
            return (T) this.ConvertToType(typeof(T));
        }

        public string GetAsJSON(string key)
        {
            int indent = 0;
            return this.GetAsJSON(key, indent);
        }

        public string GetAsJSON(string key, int indent)
        {
            return this.Internal_GetKey(key).ToJSON(indent);
        }

        public string GetAsJSON(int i)
        {
            int indent = 0;
            return this.GetAsJSON(i, indent);
        }

        public string GetAsJSON(int i, int indent)
        {
            return this.Internal_GetIndex(i).ToJSON(indent);
        }

        public void SetFromJSON(string key, string value)
        {
            this.Internal_SetKey(key, this.ParseJSON(value));
        }

        public void SetFromJSON(int i, string value)
        {
            this.Internal_SetIndex(i, this.ParseJSON(value));
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern string ToJSON(int indent);
        public string ToJSON()
        {
            int indent = 0;
            return this.ToJSON(indent);
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public override extern string ToString();
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern WebScriptObject ParseJSON(string json);
        public static implicit operator bool(WebScriptObject o)
        {
            if ((o != null) && o.IsValid())
            {
                return o.Convert<bool>();
            }
            return false;
        }

        public static implicit operator int(WebScriptObject o)
        {
            return o.Convert<int>();
        }

        public static implicit operator float(WebScriptObject o)
        {
            return o.Convert<float>();
        }

        public static implicit operator double(WebScriptObject o)
        {
            return o.Convert<double>();
        }

        public static implicit operator string(WebScriptObject o)
        {
            if ((o != null) && o.IsValid())
            {
                return o.ToString();
            }
            return string.Empty;
        }
    }
}

